package org.Lin.controller;

import org.Lin.service.FriendMessageService;
import org.json.JSONObject;

import java.io.*;
import java.net.Socket;

public class FriendMessageController extends BaseController {

    private final FriendMessageService friendMessageService = new FriendMessageService();

    public void handleSendPrivateMessage(JSONObject request, Socket socket) throws IOException {
        int senderId = request.getInt("sender_id");
        int receiverId = request.getInt("receiver_id");
        String messageData = request.getString("message_data");

        boolean success = friendMessageService.sendPrivateMessage(senderId, receiverId, messageData);
        if (success) {
            JSONObject response = createSuccessResponse("私聊消息发送成功");
            sendResponse(socket, response);
        } else {
            JSONObject response = createErrorResponse("消息发送失败");
            sendResponse(socket, response);
        }
    }

    private void sendResponse(Socket socket, JSONObject response) throws IOException {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(response.toString());
    }
}
